$(function(){
    var ANNOUNCEMENT_URL = '/ent-portal-war/new_pt_uc/my_lesson/retrieveAnnouncementItem.action'
    function Announcement() {
        this.$el = $('<div style="width: 600px; height: 500px; position:relative;"></div>')
    }

    Announcement.prototype.init = function() {
        var that = this;
        this.$el.remove().empty();
        this.$el.on('mouseenter', function(){
            that.$el.find('img[name="close"]').show();
        }).on('mouseleave', function(){
            that.$el.find('img[name="close"]').hide();
        })
        $.ajax({
            url: ANNOUNCEMENT_URL,
            type: "post",
            cache: false,
            dataType: 'json', 
            success: function(rs){
                if(rs.flag == 1) {
                    var title = rs.data.name;
                    var announcementId = rs.data.announcementId;
                    var pic = rs.data.pic;

                    var $close = $('<img name="close" src="images/close1.png" style="position:absolute; right:0; top:0; width:40px; height:40px; cursor: pointer; display:none;"/>');
                    $close.on('click', function() {
                        that.destroy();
                    })

                    var $pic = $('<div style="width: 100%;height: 100%; cursor: pointer;"></div>').append('<img style="width: 100%;height: 100%" src="' + pic + '">');
                    $pic.on('click', function(){
                        window.location.href = "/ent-portal-war/new_pt_uc/announcement/showAnnouncementInfo.action?announcementId=" + announcementId;
                    })

                    that.$el.empty().append($pic).append($close)
                } else if(rs.flag == 0) {
                    alert(rs.error || '系统忙碌，请稍后再试！')
                }
            }
        })
    };

    Announcement.prototype.showDialog = function() {
        this.init()
        var that = this;
        this.dialog = dialog({
            content: this.$el,
            cancel: false,
            onclose: function () {
                that.dialog.remove()
            }
        });
        this.dialog.showModal();
    };

    Announcement.prototype.destroy = function() {
        this.dialog && this.dialog.close().remove();
        this.$el.remove();
    };

    window.Announcement = Announcement;
})